/*
 * Utils.cpp
 *
 *  Created on: 18 Jun 2011
 *      Author: allan
 */

#include "Utils.h"

// C++ includes
#include <set>

// Boost includes
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>

// GeoReact includes
#include "Database.h"

const double ElectricalCharge(string speciesName)
{
	StringVector words = SplitLineAndClean(speciesName, "[]");
	
	if(words.size() == 1) return 0;
	
	if(words[1] == "+") return +1;
	if(words[1] == "-") return -1;
	
	const string strCharge = {words[1][1], words[1][0]};
	
	const double charge = atof(strCharge.c_str());
	
	return charge;
}

const StringVector SplitLineAndClean(const string& line, const string& tokens)
{
	StringVector splitLine;
	
	boost::split(splitLine, line, boost::is_any_of(tokens));
	
	boost::erase_all(splitLine, StringVector(1, ""));
	
	return splitLine;
}

template<class Vector> 
ostream&
PrintOperator(ostream& out, const Vector& vec)
{
	BOOST_FOREACH(const typename Vector::value_type val, vec)
		out << val << ", ";
	
	return out;
}

ostream& operator<<(ostream& out, const StringVector& vec)
{
	return PrintOperator(out, vec);
}

ostream& operator<<(ostream& out, const DoubleVector& vec)
{
	return PrintOperator(out, vec);
}

ostream& operator<<(ostream& out, const IndexVector& vec)
{
	return PrintOperator(out, vec);
}
